<?php 
/**
 * alltosun.com 第三方登录用户系统 index.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址:   http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 张元昊 (zhangyh@alltosun.com) $
 * $Date: 2015-1-24 下午3:22:46 $
 * $Id$
*/
class Action{
    
    private $user_id;
    
    public function __construct()
    {
        $this->user_id = user_helper::get_user_id();
    }
    
    public function __call($action = '', $param = array()) {
        
    }

    public function choose_bind()
    {
        if(!isset($_SESSION['associatedConnectInfo']) || empty($_SESSION['associatedConnectInfo'])) {
            return array('非法参数[1]！', 'error', AnUrl('user/login'));
        }
        if($this->user_id) {
            return array('您已经登录过了！', 'error', user_helper::get_jump_url());
        }
        $connect_info = $_SESSION['associatedConnectInfo'];
        $connect_user_info = $connect_info['user_info'];

        $login_setting = openapi_config::$login_setting;
        $connect_user = _uri($login_setting[$connect_info['type']][1], array('id' => $connect_info['id']));
        if(!$connect_user) {
            return array('非法参数[2]！', 'error', AnUrl('user/login'));
        }
        Response::assign('connect_type', $login_setting[$connect_info['type']][0]);
        Response::assign('connect_user_info', $connect_user_info);
        display_template('choose_bind.html');
    }
    
    public function bind_new() {
        if(!isset($_SESSION['associatedConnectInfo']) || empty($_SESSION['associatedConnectInfo'])) {
            return array('非法参数[1]！', 'error', AnUrl('user/login'));
        }
        if($this->user_id) {
            return array('您已经登录过了！', 'error', user_helper::get_jump_url());
        }

        $connect_info = $_SESSION['associatedConnectInfo'];
        $connect_user_info = $connect_info['user_info'];

        //随机生成一个用户名
        $username = 'N'.$connect_info['type'].$connect_info['id'];
        $hash     = user_helper::random_hash(6);
        $user_info = array(
            'user_name' => $username,
            'user_from' => $connect_info['type'],
            'avatar'   => STATIC_URL.'/images/checkin_default.jpg'
            );
        $login_setting = openapi_config::$login_setting;
        $user_id = _model('user')->create($user_info);
        if($user_id) {
            $return_info = user_helper::uc_register($username, '', '', '', '', 8);
            
            if($return_info['info'] == 'ok') {
                _model('user')->update($user_id, array('uc_id' => $return_info['uc_id']));
                _model('role_user')->create(array('role_id' => '2', 'user_id' => $user_id));
                _model($login_setting[$connect_info['type']][1])->update($connect_info['id'], array('user_id' => $user_id));

                //登录
                $user_info = _uri('user', $user_id);
                user_helper::session_update($user_info, $connect_info['type']);
                $ucenter_info = user_helper::uc_login($return_info['uc_id'], '', AnUrl('openapi/bind_success'),0,1);
                if($ucenter_info['info'] == 'error') {
                    user_helper::remember_me_expire();
                    return array($ucenter_info['msg'], 'error', AnUrl('user/login'));
                } else {
                    echo $ucenter_info;
                }
            } else {
                _model('user')->delete(array('id'=>$user_id));
                _model('role_user')->delete(array('user_id' => $user_id));
                return array('注册失败，请重新尝试', 'error', AnUrl('openapi/bind_new'));
            }
        }


        // display_template('bind_new.html');
    }

    public function bind_success() {
        if(!$this->user_id) {
            return array('您还没登录！', 'error', user_helper::get_jump_url());
        }
        if(!isset($_SESSION['associatedConnectInfo']) || empty($_SESSION['associatedConnectInfo'])) {
            Response::redirect(AnUrl('index'));
        }

        $connect_info = $_SESSION['associatedConnectInfo'];
        $connect_user_info = $connect_info['user_info'];
        unset($_SESSION['associatedConnectInfo']);
        $user_info = _uri('user', $this->user_id);

        $login_setting = openapi_config::$login_setting;
        Response::assign('user_form', $login_setting[$connect_info['type']][0]);

        Response::assign('user_info', $user_info);
        Response::assign('connect_info', $connect_info);
        display_template('bind_success.html');
    }
    
    public function bind() {
        if($this->user_id) {
            return array('您已经登录过了！', 'error', user_helper::get_jump_url());
        }
        if(!isset($_SESSION['associatedConnectInfo']) || empty($_SESSION['associatedConnectInfo'])) {
            return array('非法参数[1]！', 'error', AnUrl('user/login'));
        }
        display_template('bind.html');
    }
    
    public function create_user() {
        if(!isset($_SESSION['associatedConnectInfo']) || empty($_SESSION['associatedConnectInfo'])) {
            return array('非法参数[1]！', 'error', AnUrl('user/login'));
        }
        if($this->user_id) {
            return array('您已经登录过了！', 'error', user_helper::get_jump_url());
        }
        $username = trim(Request::Post('username', ''));
        $email    = trim(Request::Post('email', ''));
        $mobile   = trim(Request::Post('mobile', ''));
        $code     = trim(Request::Post('code', ''));
        $captcha  = trim(Request::Post('captcha', ''));

        $connect_info = $_SESSION['associatedConnectInfo'];
        $connect_user_info = $connect_info['user_info'];

        $login_setting = openapi_config::$login_setting;
        $connect_user = _uri($login_setting[$connect_info['type']][1], array('id' => $connect_info['id']));

        if(!$connect_user) {
            return array('非法参数[2]！', 'error', AnUrl('user/login'));
        } else {
            if($connect_user['user_id']) {
                return array('您已经绑定过NBD帐号了，请直接登录。', 'error', AnUrl('user/login'));
            }
        }
        if(!user_helper::is_username_len($username)) {
            return array('您提交的用户名长度不符合要求！', 'error', AnUrl('openapi/bind_new'));
        }
        if(user_helper::is_email($username)) {
            return array('您提交的用户名不能是邮箱！', 'error', AnUrl('openapi/bind_new'));
        }
        if(!user_helper::is_username_unique($username)) {
            return array('您提交的用户名已经注册！', 'error', AnUrl('openapi/bind_new'));
        }
        if(user_helper::is_number($username)) {
            return array('您提交的用户名不能为纯数字！', 'error', AnUrl('openapi/bind_new'));
        }
        $info = array('user_name' => $username);
        if(!empty($mobile)){
            if(!user_helper::is_mobile($mobile)) {
                return array('您提交的手机号的格式不正确！', 'error', AnUrl('openapi/bind_new'));
            }
            if(!user_helper::is_mobile_unique($mobile)) {
                return array('您提交的手机号已经被注册！', 'error', AnUrl('openapi/bind_new'));
            }
            if(empty($code)) {
                return array('您提交的验证码已经不能为空！', 'error', AnUrl('openapi/bind_new'));
            }
            $return_code = user_helper::getMobileCode($mobile, 'perfect');
            if($return_code['code']) {
                if($code != $return_code['code']) {
                    return array('您提交的手机验证码不正确,请重新发送！', 'error', AnUrl('openapi/bind_new'));
                }
            } else {
                return array('您提交的手机验证码不正确,请重新发送！', 'error', AnUrl('openapi/bind_new'));
            }
            //手机注册，论坛用户的用户组恢复正确
            $bbs_grounpid = 10;
            _model('mobile_code')->update(array('mobile' => $mobile, 'code' => $code, 'res_name' => 'perfect'), array('status' => 1));
            $info['mobile'] = $mobile;
        } elseif(!empty($email)) {
            if(!user_helper::is_email($email)) {
                return array('您提交的邮箱格式不正确！', 'error', AnUrl('openapi/bind_new'));
            }
            if(!user_helper::is_email_unique($email)) {
                return array('您提交的邮箱已经被注册！', 'error', AnUrl('openapi/bind_new'));
            }
            if (empty($captcha)) {
                return array('请输入验证码', 'error', AnUrl('openapi/bind_new'));
            }

            if (empty($_SESSION['securimage_code_value'])) {
                return array('验证码已过期，请点击图片刷新', 'error', AnUrl('openapi/bind_new'));
            }

            $captcha = strtolower($captcha);
            $code    = strtolower($_SESSION['securimage_code_value']);

            if ($captcha != $code) {
                return array('请按照图片中的字符正确填写验证码，不区分大小写', 'error', AnUrl('openapi/bind_new'));
            } 
            //论坛注册，论坛用户的用户组等待邮箱验证后恢复正确
            $bbs_grounpid = 8;
            $info['email'] = $email;
        } else {
            return array('您提交的数据有误！', 'error', AnUrl('openapi/bind_new'));
        }
        $info['is_perfect'] = 1;
        $info['avatar']     = $connect_user_info['avatar'];
        $info['nickname']   = $connect_user_info['user_name'];
        $info['user_from']  = $connect_info['type'];
        $info['last_login_time'] = date('Y-m-d H:i:s', time());
        $info['is_perfect'] = 1;
        $info['hash']       = user_helper::random_hash(6);
        $user_id = _model('user')->create($info);
        if($user_id) {
            $return_info = user_helper::uc_register($username, '', '', '', $mobile, $bbs_grounpid);
            if($return_info['info'] == 'ok') {
                _model('user')->update($user_id, array('uc_id' => $return_info['uc_id']));
                // 本站登录，写入session
                $user_info = _uri('user', $user_id);
                user_helper::session_update($user_info, $connect_info['type']);

                _model($login_setting[$connect_info['type']][1])->update($connect_info['id'], array('user_id' => $user_id));
                
                _model('role_user')->create(array('role_id' => '2', 'user_id' => $user_id));

                $ucenter_info = user_helper::uc_login($return_info['uc_id'], '', AnUrl('openapi/bind_success'),0,1);
                if($ucenter_info['info'] == 'error') {
                    user_helper::remember_me_expire();
                    return array($ucenter_info['msg'], 'error', AnUrl('user/login'));
                } else {
                    if(!empty($email)) {
                        user_helper::set_mail_send($email, $user_id, 'perfect');
                    }
                    echo $ucenter_info;
                }
            } else {
                _model('user')->delete(array('id'=>$user_id));
                _model('role_user')->delete(array('user_id' => $user_id));
                return array($return_info['msg'], 'error', AnUrl('openapi/bind_new'));
            }
        } else {
            return array('不好意思，注册失败，请重新注册。', 'error', AnUrl('user/register'));
        }
    }


    public function check_bind()
    {
        if ($this->user_id > 0) {
            return array('您已经登录了！', 'error', AnUrl(''));
        }
        $username = trim(Request::Post('username', ''));
        $password = trim(Request::Post('password', ''));
        $connect_info = $_SESSION['associatedConnectInfo'];
        $connect_user_info = $connect_info['user_info'];

        $login_setting = openapi_config::$login_setting;
        $connect_user = _uri($login_setting[$connect_info['type']][1], array('id' => $connect_info['id']));

        if(!$connect_user) {
            return array('非法参数[2]！', 'error', AnUrl('user/login'));
        } else {
            if($connect_user['user_id']) {
                return array('您已经绑定过NBD帐号了，请直接登录。', 'error', AnUrl('user/login'));
            }
        }

        if(empty($username)) {
            if(Request::isAjax()) {
                return array('info' => 'error', 'type' => 'user', 'msg' => '您输入的用户名不能为空');
            }
            // var_dump($username);
            // exit();
            return array('对不起，您输入的用户名不能为空', 'error', AnUrl('openapi/bind'));
        }
        
        if(empty($password)) {
            if(Request::isAjax()) {
                return array('info' => 'error', 'type' => 'pass', 'msg' => '您输入的密码不能为空');
            }
            return array('对不起，您输入的密码不能为空', 'error', AnUrl('openapi/bind'));
        }

        $get_way = user_helper::get_user_login_way($username);
        switch ($get_way) {
            case '1':
                $filter['email'] = $username;
                break;
            case '2':
                $filter['mobile'] = $username;
                break;
        
            default:
                $filter['user_name'] = $username;
                break;
        }
        
        
        $user_info = _uri('user', $filter);
        if (empty($user_info)) {
            if(Request::isAjax()) {
                return array('info' => 'error', 'type' => 'pass', 'msg' => '您输入的用户名不存在');
            }
            return array('您输入的用户名不存在', 'error', AnUrl('openapi/bind'));
        }
        $filter['password'] = user_helper::md5_password($password, $user_info['hash']);
        if ($filter['password'] != $user_info['password']) {
            if(Request::isAjax()) {
                return array('info' => 'error', 'type' => 'pass', 'msg' => '您输入的账户或者密码错误');
            }
            return array('您输入的账户或者密码错误', 'error', AnUrl('openapi/bind'));
        }
        if ($user_info['status'] == 0) {
            if(Request::isAjax()) {
                return array('info' => 'error', 'type' => 'pass', 'msg' => '登录失败，你的帐户不存在');
            }
            return array('绑定失败，你的帐户不存在', 'error', AnUrl('openapi/bind'));
        }
        $connect_weibo = _uri($login_setting[$connect_info['type']][1], array('user_id' => $user_info['id']));
        if($connect_weibo) {
            if(Request::isAjax()) {
                return array('info' => 'error', 'type' => 'pass', 'msg' => '您输入的帐号已经绑定'.$login_setting[$connect_info['type']][0].'了。');
            }
            return array('您输入的帐号已经绑定'.$login_setting[$connect_info['type']][0].'了。', 'error', AnUrl('openapi/bind'));
        }

        if(Request::isAjax()) {
            return array('info' => 'success');
        }
        _model($login_setting[$connect_info['type']][1])->update($connect_info['id'], array('user_id' => $user_info['id']));


        // 更新session
        user_helper::session_update($user_info, $connect_info['type']);
        
        $return_info = user_helper::uc_login($user_info['user_name'], $password, AnUrl('openapi/bind_success'));
        if($return_info['info'] == 'error') {
            return array($return_info['msg'], 'error', AnUrl('user/login'));
        } else {
            echo $return_info;
        }
    }
    
}

?>